<template>
  <div>
      <Breadcrmb title1="首页" title2="商品管理" title3="商品列表"></Breadcrmb>

        <el-card style="margin-top:20px">
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getGood" >
                    <el-button slot="append" icon="el-icon-search" @click="getGood"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary"  @click="add">添加商品</el-button>
                </el-col>
            </el-row>

            <el-table :data="tableData" style="width: 100%">
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="goods_name" label="商品名称" width="700px">
                </el-table-column>
                <el-table-column prop="goods_price" label="商品价格">
                </el-table-column>
                <el-table-column prop="goods_weight" label="商品重量">
                </el-table-column>
                <el-table-column prop="add_time" label="创建时间">
                    <template scope="scope">{{date(scope.row.add_time)}}</template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                  <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
                  <el-button size="mini" type="danger" icon="el-icon-delete" @click='del(scope.row)'></el-button>
                  </template>
                </el-table-column>
             </el-table>

            <el-pagination @size-change="handleSizeChange" style="text-align: center;" :page-sizes="[5, 10, 15]" @current-change="handleCurrentChange" :page-size="queryInfo.pagesize" :current-page="queryInfo.pagenum" layout="total,sizes, prev, pager, next,jumper" :total="total">
            </el-pagination>

        </el-card>
  </div>
</template>

<script>
import {getGoodList,getDeleteGood} from '@/api/good.js'

export default {
     created(){
       this.getGood();
    },
    methods:{
        add(){
            const vm = this;
            vm.$router.push({name: 'Add', params: { id: 10 }})
        },
        handleEdit(row){
            this.$router.push('/updategoods/'+row.goods_id)
        },
        //删除
        async del(row){
            // console.log(row.id);
            var {data:res}=await getDeleteGood(`goods/${row.id}`);
            console.log(res);
            if(res.meta.status !==200){
                return this.$message.error("删除失败！")
            }
            this.$message.success("删除成功");
            this.getGood();
        },
        async getGood(){
             var {data:res}=await getGoodList(this.queryInfo);
            console.log(res);
            if(res.meta.status !==200){
                return this.$message.error("获取用户列表失败！")
            }
            this.tableData=res.data.goods;
            this.total=res.data.total;
        },
        handleSizeChange(size) {
            this.queryInfo.pagesize = size;
            this.getGood();
        },
        handleCurrentChange(num) {
            this.queryInfo.pagenum = num;
            this.getGood();
        }, 
        date(time){
          //把getTime()转年月日时分秒
           var date= new Date(time);
          var yy=date.getFullYear();
          var MM=date.getMonth()+1;
          var dd=date.getDate();
          var HH=date.getHours();
          var mm=date.getMinutes();
          var ss=date.getSeconds();
          return `${yy}-${MM}-${dd} ${HH}:${mm}:${ss}`;
        }
    },
    data(){
        return {
            searchInput:'',
            tableData:[],
            queryInfo:{
                pagesize:10,
                pagenum:1,
                query:''
            },
            total: 0, //总记录数
            dialogVisible2:false,
            dialogVisible: false,
        }
    }
}
</script>

<style>
</style>